package hongcaosp.app.android.login;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blackchopper.loginshare.impl.LoginShare;
import com.blackchopper.loginshare.interfaces.OnLoginshareListener;
import com.blackchopper.loginshare.model.Type;

import hongcaosp.app.android.MainActivity;
import hongcaosp.app.android.R;
import xlj.lib.android.base.component.BaseActivity;
import xlj.lib.android.base.toastcompat.ToastManager;
import xlj.lib.android.base.utils.StatusBarUtil;

public class PasswordLoginActivity extends BaseActivity implements ILoginView,ICodeView,OnLoginshareListener {

    private EditText phoneET;
    private EditText passwordET;
    private TextView loginBtn;

    private LoginPresenter loginPresenter;
    private LoginShare loginShare;
    @Override
    protected void initData(Intent intent) {

    }

    @Override
    protected void initView() {
        loginShare = new LoginShare(this);
        loginShare.register(this);
        StatusBarUtil.setStatusBarDarkTheme(this,false);
        findViewById(R.id.close_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        phoneET = findViewById(R.id.phone_et);
        passwordET = findViewById(R.id.password_et);
        loginBtn = findViewById(R.id.login_btn);
        phoneET.addTextChangedListener(textWatcher);
        passwordET.addTextChangedListener(textWatcher);
        findViewById(R.id.register_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(PasswordLoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
        findViewById(R.id.password_login_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(PasswordLoginActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });


        loginPresenter = new LoginPresenter(this,this);

        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginAction();
            }
        });


        findViewById(R.id.forget_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                forgetPassword();
            }
        });

        findViewById(R.id.btn_wechat).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginShare.launchWechatLogin();
            }
        });
    }
    private boolean sendCode = false;
    private void forgetPassword() {
        if (sendCode) return;
        String phone = phoneET.getText().toString();
        if (phone.length() < 11) {
            ToastManager.getInstance().showToast("手机号长度不够");
            return;
        }
        loginPresenter.sendPasswordCode(phone);
        sendCode = true;
    }

    private void loginAction() {
        String phone = phoneET.getText().toString();
        String password = passwordET.getText().toString();
        if (phone.length() < 11) {
            ToastManager.getInstance().showToast("手机号长度不够");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            ToastManager.getInstance().showToast("请输入密码");
            return;
        }
        loginPresenter.loginWithPassword(phone,password);

    }

    public static void toLogin(Context activity) {
        if (activity != null) {
            Intent intent = new Intent(activity,PasswordLoginActivity.class);
            activity.startActivity(intent);
        }
    }

    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (phoneET.getText().toString().trim().length() > 0 && passwordET.getText().toString().trim().length() > 0) {
                loginBtn.setBackgroundResource(R.drawable.login_btn_blue);
                loginBtn.setTextColor(getResources().getColor(R.color.white));
            } else {
                loginBtn.setBackgroundResource(R.drawable.btn_login);
                loginBtn.setTextColor(getResources().getColor(R.color.color_blue));
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        loginShare.unRegister();
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_password_login;
    }

    @Override
    public void onLogged(boolean isOk, String message) {
        if (isOk) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        } else {
            ToastManager.getInstance().showToast(message);
        }
    }

    @Override
    public void onCodeSend(boolean success) {
        sendCode = false;
        if (success) {
            String phone = phoneET.getText().toString();
            PasswordResetActivity.toActivity(this,phone);
        }
    }

    @Override
    public void onLoginSuccess(Type type, String code, String info) {
        loginPresenter.loginWithWechat(info);
    }

    @Override
    public void onLoginCancel(Type type) {
        ToastManager.getInstance().showToast("授权取消");
    }

    @Override
    public void onLoginError(Type type, int errorCode) {
        ToastManager.getInstance().showToast("授权失败");
    }

    @Override
    public void onShareSuccess(Type type) {

    }

    @Override
    public void onShareCancel(Type type) {

    }

    @Override
    public void onShareError(Type type, int code) {

    }
}
